package com.linkzd.iot.service.Impl;

import com.linkzd.iot.domain.DeviceDataLog;
import com.linkzd.iot.domain.DeviceInfo;
import com.linkzd.iot.domain.RoomInfo;
import com.linkzd.iot.mapper.DeviceDataLogMapper;
import com.linkzd.iot.mapper.IndexMapper;
import com.linkzd.iot.model.ActuatorOnlineOrOffline;
import com.linkzd.iot.model.DeviceModel;
import com.linkzd.iot.model.DeviceOverview;
import com.linkzd.iot.model.DeviceResult;
import com.linkzd.iot.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

//import com.linkzd.iot.utils.mqtt.client.MqttPushClient;

/**
 * 设备信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-18
 */
@Service
public class IndexServiceImpl implements IIndexService
{
    @Autowired
    private IndexMapper indexMapper;

    @Autowired
    private DeviceDataLogMapper deviceDataLogMapper;
    /**
     * @Author Wangym
     * @Date 2024/5/29 下午 11:04
     * @Description 大屏设备总览查询
     * @Param
     * @Return
     **/
    @Override
    public DeviceOverview selectDeviceOverview() {
        List<DeviceInfo> deviceInfoList = indexMapper.selectDeviceOverview();
        int deviceCount = deviceInfoList.size();
        int failureCount = 0;
        int offlineCount = 0;
        for (DeviceInfo deviceInfo : deviceInfoList){
            //离线设备数量统计
            if(deviceInfo.getType().getDeviceVariety() == 1 && "0".equals(deviceInfo.getDeviceData())){
                offlineCount ++;
            }
            //故障设备数量统计
            if(deviceInfo.getDeviceState() == 0){
                failureCount++;
            }
        }
        return new DeviceOverview(deviceCount,failureCount,offlineCount);
    }

    /**
     * @Author Wangym
     * @Date 2024/5/29 下午 11:04
     * @Description 执行器在线离线数
     * @Param
     * @Return
     **/
    @Override
    public List<ActuatorOnlineOrOffline> onlineOrOffline() {
        List<ActuatorOnlineOrOffline> actuatorOnlineOrOfflineList = new ArrayList<ActuatorOnlineOrOffline>();
        List<DeviceInfo> deviceInfoList = indexMapper.selectDeviceOverview();
        int online = 0;
        int Offline = 0;
        for (DeviceInfo deviceInfo : deviceInfoList){
            if(deviceInfo.getType().getDeviceVariety() == 1){
                if("0".equals(deviceInfo.getDeviceData())){
                    Offline++;
                }else{
                    online++;
                }
            }
        }
        ActuatorOnlineOrOffline actuatorOnlineOrOffline1 = new ActuatorOnlineOrOffline("在线",online);
        ActuatorOnlineOrOffline actuatorOnlineOrOffline2 = new ActuatorOnlineOrOffline("离线",Offline);
        actuatorOnlineOrOfflineList.add(actuatorOnlineOrOffline1);
        actuatorOnlineOrOfflineList.add(actuatorOnlineOrOffline2);
        return actuatorOnlineOrOfflineList;
    }

    /**
     * @Author Wangym
     * @Date 2024/5/29 下午 11:04
     * @Description 查询平台下发设备的开关在线离线动态
     * @Param
     * @Return
     **/
    @Override
    public List<DeviceDataLog> getDataLog() {
        return deviceDataLogMapper.selectDeviceDataLogList(new DeviceDataLog());
    }

    /**
     * 查询设备信息列表
     *
     * @param roomInfo 设备信息
     * @return 设备信息
     */
    @Override
    public DeviceResult selectDeviceInfoList(RoomInfo roomInfo)
    {
        List<DeviceInfo> deviceInfoList = indexMapper.selectDeviceInfoList(roomInfo);
        List<DeviceModel> actuator = new ArrayList<>();//执行器
        List<DeviceModel> sensor = new ArrayList<>();//传感器
        for (DeviceInfo deviceInfo:deviceInfoList){
            String typeId = String.valueOf(deviceInfo.getType().getDeviceVariety());
            String deviceData = deviceInfo.getDeviceData();
            String type = String.valueOf(deviceInfo.getType().getDeviceType());
            //装
            DeviceModel deviceModel = new DeviceModel();
            deviceModel.setDeviceTypeId(deviceInfo.getDeviceTypeId());
            String deviceCode = deviceInfo.getDeviceCode();
            deviceModel.setDeviceId(deviceInfo.getDeviceId());
            deviceModel.setDeviceCode(deviceCode);
            deviceModel.setDeviceType(Long.valueOf(type));
            deviceModel.setRoomNum(deviceInfo.getRoom().getRoomNum());
            String deviceName = getDeviceName(type);

            //传感器
            if("0".equals(typeId)){
                deviceModel.setDeviceName(deviceName);
                if("14".equals(type)){//温湿度
                    DeviceModel deviceModel2 = null;
                    try {
                        deviceModel2 = (DeviceModel) deviceModel.clone();
                    } catch (CloneNotSupportedException e) {
                        e.printStackTrace();
                    }
                    String[] TH = deviceData.split(",");
                    String temperature = TH[0].split(":")[1];
                    String humidness = TH[1].split(":")[1];
                    deviceModel.setDeviceName("湿度");
                    deviceModel.setDeviceData(humidness);
                    deviceModel2.setDeviceName("温度");
                    deviceModel2.setDeviceData(temperature);
                    sensor.add(deviceModel2);
                }else {
                    String value = deviceData.split(":")[1];
                    deviceModel.setDeviceData(value);
                }

                sensor.add(deviceModel);
            }else if("1".equals(typeId)){//执行器
                //deviceCode.substring(2)设备位置
                deviceName = deviceName + deviceCode.substring(5);
                deviceModel.setDeviceName(deviceName);
                deviceModel.setDeviceData(deviceData);
                actuator.add(deviceModel);
            }
        }
        DeviceResult deviceResult = new DeviceResult(actuator,sensor);
        return deviceResult;
    }
    private String getDeviceName(String type){
        String name = "";
        switch (type){
            case "10":
                name = "风扇";
                break;
            case "11":
                name = "LED灯";
                break;
            case "12":
                name = "加热器";
                break;
            case "13":
                name = "水泵";
                break;
            case "15":
                name = "光照值";
                break;
            case "16":
                name = "土壤温度";
                break;
            case "17":
                name = "雨滴值";
                break;
        }
        return name;
    }
}
